//
//  PhotoViewerCell.swift
//  Weibo
//
//  Created by apple on 15/7/4.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

protocol PhotoViewerCellDelegate: NSObjectProtocol {
    func photoViewerCellDidTapImage()
}


class PhotoViewerCell: UICollectionViewCell, UIScrollViewDelegate {
    
    weak var photoDelegate: PhotoViewerCellDelegate?
    var imageURL: NSURL? {
        didSet {
            
            indicator.startAnimating()
            resetScrollView()//重置清零 然后重新计算正确的
            // 设置图像 - 错误的方法，异步加载图像，没有大小
            
            imageView.sd_setImageWithURL(imageURL!, placeholderImage: nil) { (image, error, _, _) in
                
                self.imagePostion(image)
                self.indicator.stopAnimating()
            }
        }
    }
    
    ///  重设 scrollView 内容参数
    private func resetScrollView() {
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSizeZero
    }
    /// 设置位置
    private func imagePostion(image: UIImage) {
        
        // 计算 y 值
        let size = displaySize(image)
        
        // 判断是否是长图
        if bounds.height < size.height {
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            scrollView.contentSize = size
        } else {
            // 短图
            let y = (bounds.height - size.height) * 0.5
            // 设置 scrollView 的边距
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            scrollView.contentInset = UIEdgeInsets(top: y, left: 0, bottom: 0, right: 0)
        }
    }
    
    ///  根据图像计算显示的尺寸
    private func displaySize(image: UIImage) -> CGSize {
        
        let scale = image.size.height / image.size.width
        let h = bounds.width * scale
        
        return CGSize(width: bounds.width, height: h)
    }
    
    // MARK:scrollview的代理方法
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        
        
        // 重新计算边距
        var offsetX = (scrollView.bounds.width - view!.frame.width) * 0.5
        if offsetX < 0 {
            offsetX = 0
        }
        var offsetY = (scrollView.bounds.height - view!.frame.height) * 0.5
        if offsetY < 0 {
            offsetY = 0
        }
        // 重新设置边距
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
    }
    func clickImage() {
        photoDelegate?.photoViewerCellDidTapImage()
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        scrollView.frame = bounds
        addSubview(scrollView)
        scrollView.delegate = self
        scrollView.minimumZoomScale = 0.5
        scrollView.maximumZoomScale = 2.0
        //添加图片 手势 开启用户交互
        scrollView.addSubview(imageView)
        let tap = UITapGestureRecognizer(target: self, action: "clickImage")
        imageView.addGestureRecognizer(tap)
        imageView.userInteractionEnabled = true
        
        
        addSubview(indicator)
        indicator.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil)
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 懒加载控件
    lazy private var scrollView = UIScrollView()
    lazy var imageView = UIImageView()
    lazy private var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    

}
